﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.BLL;
using KeRui.Common;
using System.Text;

public partial class Manage_Cms_Fielditem : System.Web.UI.Page
{
    private ManageErrMsg nsb = new ManageErrMsg();
    private readonly string Act = ManageHelper.PageAct();
    protected List<KeRui.Model.CmsModel> listCmsModel;
    protected KeRui.Model.CmsField modelCmsFieldShow = new KeRui.Model.CmsField();
    protected void Page_Load(object sender, EventArgs e)
    {
        //登录
        ManageHelper.CheckAdminLogin();
        //权限
        ManageHelper.CheckAdminPower("system_fieldslist");
        KeRui.BLL.CmsField bllCmsField = new CmsField();
        KeRui.BLL.CmsModel bllCmsModel = new KeRui.BLL.CmsModel();
        listCmsModel = bllCmsModel.GetList();
        int fieldID = PageRequest.GetInt("fieldID", -1);
        if (Act == "modify")
        {
            modelCmsFieldShow = bllCmsField.GetModel(fieldID);
        }
        switch (KeRui.Common.PageRequest.GetString("method"))
        {
            //添加Field
            case "add":
                //权限
                ManageHelper.CheckAdminPower("system_fieldslistadd");
                KeRui.Model.CmsField modelCmsField = new KeRui.Model.CmsField();
                modelCmsField.ModelID = PageRequest.GetInt("nodeiddropdownlist", -1);
                modelCmsField.FieldType = PageRequest.GetInt("fieldtype", -1);
                modelCmsField.FieldName = PageRequest.GetString("fieldname");

                //自动判断屏蔽CmsContent已经有的字段
                WithOutCmsContentFile(modelCmsField.FieldName);

                modelCmsField.FieldAlias = PageRequest.GetString("fieldalias");
                modelCmsField.Tips = PageRequest.GetString("tips");
                modelCmsField.Description = PageRequest.GetString("summary");
                modelCmsField.DefaultValue = PageRequest.GetString("txtDefaultValue");
                modelCmsField.EnableNull = PageRequest.GetInt("isnull", -1) == 1 ? true : false;
                modelCmsField.IsUsing = PageRequest.GetInt("using", -1) == 1 ? true : false;
                modelCmsField.EnableSearch = PageRequest.GetInt("searchenable", -1) == 1 ? true : false;
                modelCmsField.IsTab = PageRequest.GetInt("istab", 0);
                switch (PageRequest.GetInt("fieldtype", -1))
                {
                    case 1:
                        modelCmsField.MaxValue = PageRequest.GetString("txtMaxLength");
                        modelCmsField.StyleValue = PageRequest.GetString("txtWidth");
                        break;
                    case 2:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("showwidth") + "-" + PageRequest.GetString("showheight");
                        break;
                    case 3:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("showwidth") + "-" + PageRequest.GetString("showheight");
                        break;
                    case 4:
                    case 7:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("selectedlist");
                        break;
                    case 8:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = "";
                        break;
                    case 9:
                        modelCmsField.MaxValue = "";
                        switch (PageRequest.GetInt("DateTimeDefaultValue", -1))
                        {
                            case 0:
                                modelCmsField.DefaultValue = "";
                                break;
                            case 1:
                                modelCmsField.DefaultValue = DateTime.Now.ToShortDateString();
                                break;
                            case 2:
                                modelCmsField.DefaultValue = "";
                                break;
                        }
                        modelCmsField.StyleValue = PageRequest.GetString("DateTimeDefaultValue");
                        break;
                    case 10:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = "";
                        break;
                    case 11:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("fieldplusattrvalue");
                        break;
                }
                bllCmsField.Add(modelCmsField);
                //日志HYC
                ManageHelper.AddLog("system_fieldslistadd", "添加了字段ID为" + modelCmsField.FieldName);
                Response.Write(
                            string.Format(
                                "<script type=\"text/javascript\">window.parent.location.href='FieldList.aspx?siteid={0}&modelid={1}'</script>",
                                PageRequest.GetInt("siteid", -1), modelCmsField.ModelID));
                Response.End();
                break;
            //修改Field
            case "modify":
                //权限HYC
                ManageHelper.CheckAdminPower("system_fieldslistupdate");
                modelCmsField = new KeRui.Model.CmsField();
                modelCmsField.FieldID = fieldID;
                modelCmsField.ModelID = PageRequest.GetInt("nodeiddropdownlist", -1);
                modelCmsField.FieldType = PageRequest.GetInt("fieldtypehidden", -1);
                modelCmsField.FieldName = PageRequest.GetString("fieldname");

                //自动判断屏蔽CmsContent已经有的字段
                WithOutCmsContentFile(modelCmsField.FieldName);

                modelCmsField.FieldAlias = PageRequest.GetString("fieldalias");
                modelCmsField.Tips = PageRequest.GetString("tips");
                modelCmsField.Description = PageRequest.GetString("summary");
                modelCmsField.DefaultValue = PageRequest.GetString("txtDefaultValue");
                modelCmsField.EnableNull = PageRequest.GetInt("isnull", -1) == 1 ? true : false;
                modelCmsField.IsUsing = PageRequest.GetInt("using", -1) == 1 ? true : false;
                modelCmsField.EnableSearch = PageRequest.GetInt("searchenable", -1) == 1 ? true : false;
                modelCmsField.IsTab = PageRequest.GetInt("istab", 0);
                switch (PageRequest.GetInt("fieldtypehidden", -1))
                {
                    case 1:
                        modelCmsField.MaxValue = PageRequest.GetString("txtMaxLength");
                        modelCmsField.StyleValue = PageRequest.GetString("txtWidth");
                        break;
                    case 2:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("showwidth") + "-" + PageRequest.GetString("showheight");
                        break;
                    case 3:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("showwidth") + "-" + PageRequest.GetString("showheight");
                        break;
                    case 4:
                    case 7:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("selectedlist");
                        break;
                    case 8:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = "";
                        break;
                    case 9:
                        modelCmsField.MaxValue = "";
                        switch (PageRequest.GetInt("DateTimeDefaultValue", -1))
                        {
                            case 0:
                                modelCmsField.DefaultValue = "";
                                break;
                            case 1:
                                modelCmsField.DefaultValue = DateTime.Now.ToShortDateString();
                                break;
                            case 2:
                                modelCmsField.DefaultValue = "";
                                break;
                        }
                        modelCmsField.StyleValue = PageRequest.GetString("DateTimeDefaultValue");
                        break;
                    case 10:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = "";
                        break;
                    case 11:
                        modelCmsField.MaxValue = "";
                        modelCmsField.StyleValue = PageRequest.GetString("fieldplusattrvalue");
                        break;
                }
                bllCmsField.Update(modelCmsField);
                //日志HYC
                ManageHelper.AddLog("system_fieldslistupdate", "修改了字段" + modelCmsField.FieldName);
                Response.Write(
                            string.Format(
                                "<script type=\"text/javascript\">window.parent.location.href='FieldList.aspx?siteid={0}&modelid={1}'</script>",
                                PageRequest.GetInt("siteid", -1), modelCmsField.ModelID));
                Response.End();
                break;
        }
    }

    protected string GetFieldPlus(string selected)
    {
        StringBuilder sb = new StringBuilder();
        KeRui.BLL.CmsFieldAttr bllCmsFieldAttr = new CmsFieldAttr();
        DataTable dtClasses = bllCmsFieldAttr.GetClassList();
        foreach (DataRow row in dtClasses.Rows)
        {
            if (row["ClassName"].ToString() != selected)
            {
                sb.AppendFormat("<option value='{0}'>{0}</option>", row["ClassName"]);
            }
            else
            {
                sb.AppendFormat("<option value='{0}' selected='selected'>{0}</option>", row["ClassName"]);
            }
        }
        return sb.ToString();
    }

    #region 自动判断屏蔽CmsContent已经有的字段
    /// <summary>
    /// 自动判断屏蔽CmsContent已经有的字段
    /// </summary>
    /// <param name="FieldName">字段名</param>
    private void WithOutCmsContentFile(string FieldName) 
    {
        KeRui.BLL.TagModule bllTagModule = new KeRui.BLL.TagModule();
        ArrayList listWithOut = new ArrayList();
        StringBuilder sbWithOut = new StringBuilder();
        int tableid = 0;
        try
        {
            tableid = int.Parse(bllTagModule.GetTableIDByTableName("cmscontent"));
        }
        catch
        {
            nsb.AddErr("不存在的表名。"); nsb.ChkErr();
        }

        DataTable dtWithOut = bllTagModule.GetFieldsByTableID(tableid);

        foreach (DataRow row in dtWithOut.Rows)
        {
            listWithOut.Add(row["name"]);
        }

        if (listWithOut.IndexOf(FieldName) > 0)
        {
            nsb.AddErr("此字段名已经在主表中存在。"); nsb.ChkErr();
        }
    }
    #endregion
}